Cleaning up
[andmenj-acm.git] / 10003 - Cutting sticks / 10003.3.cpp
blob1068fe7c54126c537ca99d7f34b44b32d57e5254
1 #include <iostream>
2 #include <map>
3 #include <algorithm>
4 #include <climits>
5 #include <vector>
7 using namespace std;
9 vector<int> p;
10 int cache[1001][1001];
12 int f(int i, int j){
13 if (cache[i][j] != -1){
14 return cache[i][j];
16 if (j < i) swap(i, j);
18 for (int k=0; k<p.size()-1; ++k){
19 if (p[k] <= i && i <= p[k+1] &&
20 p[k] <= j && j <= p[k+1]){
21 return cache[i][j] = 0;
25 int min = INT_MAX;
26 for (int k=0; k<p.size(); ++k){
27 if (i < p[k] && p[k] < j){
28 int q = j - i + f(i, p[k]) + f(p[k], j);
29 if (q < min){
30 min = q;
34 return cache[i][j] = min;
38 int main(){
39 int l;
40 while (cin >> l && l > 0){
41 int n;
42 cin >> n;
43 memset(cache, -1, sizeof(cache));
44 p = vector<int>(n+2);
45 p[0] = 0;
46 p[n+1] = l;
47 for (int i=1; i<=n; ++i){
48 cin >> p[i];
50 cout << "The minimum cutting is " << f(0, l) << ".\n";
52 return 0;